package com.sfj.system.service.group;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaka.starter.token.util.HeaderUtils;
import com.sfj.common.util.BeanUtils;
import com.sfj.system.dto.group.GroupAddDTO;
import com.sfj.system.dto.group.GroupQueryDTO;
import com.sfj.system.dto.group.GroupTreeDTO;
import com.sfj.system.dto.group.GroupUpdDTO;
import com.sfj.system.entity.GroupEntity;
import com.sfj.system.mapper.GroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kaka
 * @Date 2025/06/29
 */
@Service
public class GroupService {

    @Autowired
    private GroupMapper groupMapper;

    public GroupEntity getById(String groupId) {
        return groupMapper.selectByPrimaryKey(groupId);
    }

    public IPage<GroupEntity> queryGroupListByPage(GroupQueryDTO groupQueryDTO) {
        Page<GroupEntity> page =
                new Page<>(groupQueryDTO.getPageNo(), groupQueryDTO.getPageSize());
        return groupMapper.queryGroupInfoList(page, groupQueryDTO);
    }

    //树形结构
    public List<GroupTreeDTO> queryGroupList() {
        return groupMapper.queryGroupList(null);
    }

    public List<GroupTreeDTO> queryTaskGroupList() {
        String queryGroupId = null;
        if (HeaderUtils.getUserGroupIds() == 2 ||
                HeaderUtils.getUserGroupIds() == 1) {
            queryGroupId = null;
        }else {
            queryGroupId = String.valueOf(HeaderUtils.getUserGroupIds());
        }
        return groupMapper.queryGroupList(queryGroupId);
    }

    public List<GroupTreeDTO> buildGroupTree() {
        List<GroupTreeDTO> groupTreeList = queryGroupList();
        Map<Integer, GroupTreeDTO> groupMap = new HashMap<>();

        // 初始化 map
        for (GroupTreeDTO group : groupTreeList) {
            groupMap.put(group.getGroupId(), group);
        }

        List<GroupTreeDTO> rootList = new ArrayList<>();

        // 构建树结构
        for (GroupTreeDTO group : groupTreeList) {
            Integer parentId = group.getUpGroupId();
            if (parentId == null || parentId == 0) {
                rootList.add(group); // 顶级节点
            } else {
                GroupTreeDTO parent = groupMap.get(parentId);
                if (parent != null) {
                    parent.getChildren().add(group);
                } else {
                    // 如果找不到上级，可以放顶层或忽略
                    rootList.add(group);
                }
            }
        }

        return rootList;
    }


    public void add(GroupAddDTO group) {
        GroupEntity groupEntity = new GroupEntity();
        org.springframework.beans.BeanUtils.copyProperties(group, groupEntity);
        groupEntity.setCreateTime(LocalDateTime.now().withNano(0));
        groupEntity.setUpdateTime(LocalDateTime.now().withNano(0));
        groupMapper.insert(groupEntity);
    }

    public void update(GroupUpdDTO group) {
        GroupEntity groupEntity = new GroupEntity();
        org.springframework.beans.BeanUtils.copyProperties(group, groupEntity);
        groupEntity.setUpdateTime(LocalDateTime.now().withNano(0));
        groupMapper.update(groupEntity);
    }

    public void delete(Integer groupId) {
        groupMapper.deleteByPrimaryKey(groupId);
    }

    public List<Map<Integer, String>> getGroupNameByIds(List<Integer> userIds) {
        return groupMapper.getGroupNameByIds(userIds);
    }
}
